package com.itheima.xx.xml_l;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * Description:
 *
 * @author codermao
 * @date 2023/10/23 15:36
 */
public class DomTest01 {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        File file = new File("E:\\MyCode\\java_web_learn\\maven_learn\\src\\main\\java\\com\\itheima\\xx\\xml_l\\books.xml");
        Document document = saxReader.read(file);
        Element rootElement = document.getRootElement();

        List<Element> bookElements = rootElement.elements("book");
        for (Element bookElement : bookElements) {
            String id = bookElement.attributeValue("id");
            String address = bookElement.attributeValue("address");
            System.out.println("id = " + id + ",address = " + address);

            String name = bookElement.elementText("name");
            String author = bookElement.elementText("author");
            String price = bookElement.elementText("price");
            System.out.println("name = " + name + ", author = " + author + ",price = " + price);
        }

    }
}
